package xyz.lne.researchgroupmanage.controller;


import cn.hutool.crypto.SecureUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import xyz.lne.researchgroupmanage.annotation.AnonymousGetMapping;
import xyz.lne.researchgroupmanage.annotation.AnonymousPostMapping;
import xyz.lne.researchgroupmanage.common.CommonResponse;
import xyz.lne.researchgroupmanage.controller.request.ResearchGroupRequest;
import xyz.lne.researchgroupmanage.controller.response.*;
import xyz.lne.researchgroupmanage.security.UserPower;
import xyz.lne.researchgroupmanage.service.KeywordService;
import xyz.lne.researchgroupmanage.service.ResearchGroupService;
import xyz.lne.researchgroupmanage.service.dataTransferObject.*;
import xyz.lne.researchgroupmanage.util.SecurityUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author team7
 * @since 2021-01-19
 */
@RestController
@RequestMapping("/researchgroup")
@Api(value = "课题组Controller接口测试")
public class ResearchGroupController {

    @Autowired
    private ResearchGroupService researchgroupService;

    @Autowired
    private KeywordService keywordService;

    @AnonymousPostMapping("/myResearchGroup")
    @ApiOperation("我的课题组信息")
    public List<ResearchGroupSimpleResponse> myResearchGroup(@RequestParam("userId") Integer userId) {
        List<ResearchGroupSimpleDTO> researchGroupSimpleDTOList = researchgroupService.findMyResearchGroup(userId);
        return convertResearchGroupSimpleDtoToResponse(researchGroupSimpleDTOList);
    }

    private List<ResearchGroupSimpleResponse> convertResearchGroupSimpleDtoToResponse(List<ResearchGroupSimpleDTO> researchGroupSimpleDTOList) {
        List<ResearchGroupSimpleResponse> vos = new ArrayList<>();
        researchGroupSimpleDTOList.forEach((dto) -> {
            ResearchGroupSimpleResponse vo = new ResearchGroupSimpleResponse();
            BeanUtils.copyProperties(dto, vo);
            vos.add(vo);
        });
        return vos;
    }

    @AnonymousPostMapping("searchResearchGroupInAll")
    @ApiOperation("在所有的课题组中查询课题组")
    public List<ResearchGroupSimpleResponse> searchResearchGroupInAll(@RequestParam("word") String word) {
        List<ResearchGroupSimpleDTO> researchGroupSimpleDTOList = researchgroupService.findResearchGroupInAll(word);
        return convertResearchGroupSimpleDtoToResponse(researchGroupSimpleDTOList);
    }

    @AnonymousPostMapping("searchResearchGroupInMy")
    @ApiOperation("在我的课题组中查询课题组")
    public List<ResearchGroupSimpleResponse> searchResearchGroupInMy(@RequestParam("word") String word) {
        Integer userId = SecurityUtils.getCurrentUserId();
        List<ResearchGroupSimpleDTO> researchGroupSimpleDTOList = researchgroupService.findResearchGroupInMy(userId, word);
        return convertResearchGroupSimpleDtoToResponse(researchGroupSimpleDTOList);
    }

    @AnonymousPostMapping("findUntreatedResearchApplication")
    @ApiOperation("在我的课题组中查询没有处理的课题申请信息")
    public List<ResearchApplicationResponse> findUntreatedResearchApplication(@RequestParam("researchGroupId") Integer researchGroupId) {
        List<ResearchApplicationDTO> untreatedResearchApplicationInResearchGroup = researchgroupService.findUntreatedResearchApplicationInResearchGroup(researchGroupId);
        return convertResearchApplicationDtoToResponse(untreatedResearchApplicationInResearchGroup);
    }

    private List<ResearchApplicationResponse> convertResearchApplicationDtoToResponse(List<ResearchApplicationDTO> researchApplicationDTOList) {
        List<ResearchApplicationResponse> vos = new ArrayList<>();
        researchApplicationDTOList.forEach((dto) -> {
            ResearchApplicationResponse vo = new ResearchApplicationResponse();
            BeanUtils.copyProperties(dto, vo);
            vos.add(vo);
        });
        return vos;
    }

    @AnonymousPostMapping("findUntreatedProposerApplication")
    @ApiOperation("在我的课题组中查询没有处理的申请人申请信息")
    public List<ProposerApplicationDTO> findUntreatedProposerApplication(@RequestParam("researchGroupId") Integer researchGroupId) {
        List<ProposerApplicationDTO> untreatedProposerApplicationInResearchGroup = researchgroupService.findUntreatedProposerApplicationInResearchGroup(researchGroupId);
        return convertProposerApplicationDtoToResponse(untreatedProposerApplicationInResearchGroup);
    }

    private List<ProposerApplicationDTO> convertProposerApplicationDtoToResponse(List<ProposerApplicationDTO> proposerApplicationDTOList) {
        List<ProposerApplicationDTO> vos = new ArrayList<>();
        proposerApplicationDTOList.forEach((dto) -> {
            ProposerApplicationDTO vo = new ProposerApplicationDTO();
            BeanUtils.copyProperties(dto, vo);
            vos.add(vo);
        });
        return vos;
    }

    @AnonymousPostMapping("createResearchGroup")
    @ApiOperation("创建课题组")
    public CommonResponse createResearchGroup(@RequestBody ResearchGroupRequest researchGroupRequest) {
        Integer userId = SecurityUtils.getCurrentUserId();
        Integer affected = researchgroupService.createResearchGroup(researchGroupRequest.getAccount(), researchGroupRequest.getName(), researchGroupRequest.getIntroduction(),
                researchGroupRequest.getResearchGroupImg(), userId, researchGroupRequest.getKeywordId());
        if (affected < 1) {
            return CommonResponse.create(CommonResponse.STATUS_FAIL, "创建课题组失败");
        }
        return CommonResponse.create(CommonResponse.STATUS_SUCCESS, "创建课题组成功");
    }

    @AnonymousPostMapping("updateResearchGroup")
    @ApiOperation("更新课题组")
    public CommonResponse updateResearchGroup(@RequestBody ResearchGroupRequest researchGroupRequest) {
        Integer userId = UserPower.check(UserPower.RESEARCH_GROUP_MANAGE, researchGroupRequest.getResearchGroupId());
        Integer affected = researchgroupService.updateResearchGroup(researchGroupRequest.getResearchGroupId(), researchGroupRequest.getAccount(), researchGroupRequest.getName(), researchGroupRequest.getIntroduction(),
                researchGroupRequest.getResearchGroupImg(), userId, researchGroupRequest.getKeywordId());
        if (affected < 1) {
            return CommonResponse.create(CommonResponse.STATUS_FAIL, "更新课题组失败");
        }
        return CommonResponse.create(CommonResponse.STATUS_SUCCESS, "更新课题组成功");
    }

    @AnonymousPostMapping("delete/{researchGroupId}")
    @ApiOperation("删除课题组")
    public CommonResponse updateResearchGroup(@PathVariable Integer researchGroupId) {
        UserPower.check(UserPower.RESEARCH_GROUP_MANAGE, researchGroupId);
        Integer affected = researchgroupService.deleteResearchGroupByResearchGroupId(researchGroupId);
        if (affected < 1) {
            return CommonResponse.create(CommonResponse.STATUS_FAIL, "删除课题组失败");
        }
        return CommonResponse.create(CommonResponse.STATUS_SUCCESS, "删除课题组成功");
    }

    @AnonymousGetMapping("get/{researchGroupId}")
    @ApiOperation("获取具体的课题组")
    public ResearchGroupResponse getSpecificResearchGroupInfoByResearchGroupId(@PathVariable("researchGroupId") Integer researchGroupId) {
        ResearchGroupDTO researchGroupDTO = researchgroupService.findResearchGroupById(researchGroupId);
        return convertResearchGroupDtoToResponse(researchGroupDTO);
    }

    private ResearchGroupResponse convertResearchGroupDtoToResponse(ResearchGroupDTO researchGroupDTO) {
        ResearchGroupResponse researchGroupResponse = new ResearchGroupResponse();
        ResearchGroupNormalResponse researchGroupNormalResponse = new ResearchGroupNormalResponse();
        BeanUtils.copyProperties(researchGroupDTO, researchGroupNormalResponse);
        researchGroupResponse.setResearchGroupNormalResponse(researchGroupNormalResponse);
        return researchGroupResponse;
    }

    @AnonymousPostMapping("manageUserApplication")
    @ApiOperation("处理课题组用户申请")
    public CommonResponse manageUserApplication(@RequestParam("researchGroupId") Integer researchGroupId,
                                                @RequestParam("userId") Integer userId,
                                                @RequestParam("status") Integer status) {
        Integer affected = researchgroupService.manageResearchGroupUser(researchGroupId, userId, status);
        if (affected < 1) {
            return CommonResponse.create(CommonResponse.STATUS_FAIL);
        } else {
            return CommonResponse.create(CommonResponse.STATUS_SUCCESS);
        }
    }

    @AnonymousPostMapping("manageResearchApplication")
    @ApiOperation("处理课题组课题申请")
    public CommonResponse manageResearchApplication(@RequestParam("researchId") Integer researchId,
                                                    @RequestParam("userId") Integer userId,
                                                    @RequestParam("status") Integer status) {

        Integer affected = researchgroupService.manageResearchGroupResearch(researchId, userId, status);
        if (affected < 1) {
            return CommonResponse.create(CommonResponse.STATUS_FAIL);
        } else {
            return CommonResponse.create(CommonResponse.STATUS_SUCCESS);
        }
    }

    @AnonymousGetMapping("findResearchGroupUsers/{researchGroupId}")
    @ApiOperation("查询某个课题组下的所有用户")
    public List<UserResponse> findResearchGroupUsers(@PathVariable("researchGroupId") Integer researchGroupId) {
        List<UserDTO> userDTOList = researchgroupService.findAllResearchGroupUsers(researchGroupId);
        return convertUserDtoToResponse(userDTOList);
    }

    private List<UserResponse> convertUserDtoToResponse(List<UserDTO> userDTOList) {
        List<UserResponse> userResponseList = new ArrayList<>();
        userDTOList.forEach(userDTO -> {
            UserResponse userResponse = new UserResponse();
            BeanUtils.copyProperties(userDTO, userResponse);
            userResponseList.add(userResponse);
        });
        return userResponseList;
    }

    @AnonymousGetMapping("findResearchGroupResearches/{researchGroupId}")
    @ApiOperation("查询某个课题组下的所有课题")
    public List<ResearchNormalResponse> findResearchGroupResearches(@PathVariable("researchGroupId") Integer researchGroupId) {
        List<ResearchDTO> researchDTOList = researchgroupService.findAllResearchByResearchGroupId(researchGroupId);
        return convertResearchDtoToResponse(researchDTOList);
    }

    private List<ResearchNormalResponse> convertResearchDtoToResponse(List<ResearchDTO> researchDTOList) {
        if (researchDTOList == null) {
            return new ArrayList<>();
        }
        List<ResearchNormalResponse> researchNormalResponses = new ArrayList<>();
        researchDTOList.forEach(researchDTO -> {
            ResearchNormalResponse researchNormalResponse = new ResearchNormalResponse();
            BeanUtils.copyProperties(researchDTO, researchNormalResponse);
            researchNormalResponses.add(researchNormalResponse);
        });
        return researchNormalResponses;
    }

    @AnonymousPostMapping("findUserInResearchGroup")
    @ApiOperation("找到课题组下的所有成员")
    public List<UserInResearchGroupResponse> findUserInResearchGroup(@RequestParam("researchGroupId") Integer researchGroupId) {
        List<UserInResearchGroupDTO> userInResearchGroupDTOList = researchgroupService.findUserInResearchGroup(researchGroupId);
        return convertUserInResearchGroupDtoToResponse(userInResearchGroupDTOList);
    }

    private List<UserInResearchGroupResponse> convertUserInResearchGroupDtoToResponse(List<UserInResearchGroupDTO> userInResearchGroupDTOList) {
        if (userInResearchGroupDTOList == null) {
            return new ArrayList<>();
        }
        List<UserInResearchGroupResponse> userInResearchGroupResponseList = new ArrayList<>();
        userInResearchGroupDTOList.forEach(userInResearchGroupDTO -> {
            UserInResearchGroupResponse userInResearchGroupResponse = new UserInResearchGroupResponse();
            BeanUtils.copyProperties(userInResearchGroupDTO, userInResearchGroupResponse);
            userInResearchGroupResponseList.add(userInResearchGroupResponse);
        });
        return userInResearchGroupResponseList;
    }

    @AnonymousPostMapping("applyJoinResearchGroup")
    @ApiOperation("申请加入课题组")
    public CommonResponse applyJoinResearchGroup(@RequestParam("researchGroupId") Integer researchGroupId) {
        Integer userId = SecurityUtils.getCurrentUserId();
        Integer affected = researchgroupService.applyJoinResearchGroup(userId, researchGroupId);
        if (affected <= 0) {
            return CommonResponse.create(CommonResponse.STATUS_FAIL);
        } else {
            return CommonResponse.create(CommonResponse.STATUS_SUCCESS);
        }
    }






}

